package com.fxd.dao.impl;

import com.fxd.dao.IAccountDao;
import com.fxd.domain.account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import java.util.List;

/**
 * @auther fuxudong
 * @date 2021/1/29 - 11:28
 */
public class iAccountDaoImpl extends JdbcDaoSupport implements IAccountDao {



    public account findAccountById(Integer id) {
        List<account> as = super.getJdbcTemplate().query("select*from account where id=?", new BeanPropertyRowMapper<account>(account.class), id);
        return as.isEmpty() ? null : as.get(0);
    }

    public account findAccountByName(String name) {
        List<account> as = super.getJdbcTemplate().query("select*from account where name=?", new BeanPropertyRowMapper<account>(account.class), name);
        if (as.isEmpty()) {
            return null;
        }
        if (as.size() > 1) {
            throw new RuntimeException("结果集不唯一");
        }
        return as.get(0);
    }

    public void updateAccount(account account) {
        super.getJdbcTemplate().update("update account set name =?,moeny = ? where id=?", account.getName(), account.getMoney(), account.getId());

    }
}
